<?php
/**
 * Class Lib_LrApi
 */

class Lib_LrApi{
    private static $obj = null;
    /**
     * 获取单例对象
     * @param string $hl
     * @param number $timeout
     * @return Lib_LrApi
     */
    public static function getInstance(){
        if(is_null(self::$obj)){
            self::$obj = new Lib_LrApi();
        }
        return self::$obj;
    }

    public static function getUrl (){
        $env = getenv('RUNTIME_ENVIROMENT') ? getenv('RUNTIME_ENVIROMENT') : (defined('SHELL_VARIABLE') ? SHELL_VARIABLE : '');
        $env = empty($env) ? 'local' : $env;
        if($env == 'test'){
            return "http://testrpc.gzclearing.com";
        }
        return "http://lrrpc.gzclearing.com";
    }
    /**
     * 调用修复接口
     * @param string $method
     * @param array $data
     * @return array $data
     */
    public function lossrepair($method,$data)
    {
        if (!is_string($method)) {
            return false;
        }
        $temp = explode('_', $method);
        if(!isset($temp[0]) || !isset($temp[1])) {
            return false;
        }
        $url = $temp[0];
        $method = $temp[1];
        $rpc = self::getUrl();
        try {
            $yar_client = new Lib_Yar("{$rpc}/lossrepair/{$url}.php");
            $res = $yar_client->execute($method, array($data));
            if (empty($res) or !isset($res['code'])) {
                //记录日志
                Func_Log::write('LrqApi', $url . @http_build_query($res), $res);
                //去错误页面
                return ['code'=>400,'message'=>"操作失败",'data'=>[]];
            }

            return $res;
        } catch (Exception $e) {
            return ['code'=>500,'message'=>"服务器异常",'data'=>[]];
        }
    }

    /**
     * 调用短信接口
     * @param string $method
     * @param array $data
     * @return array $data
     */
    public function sendSMS($method,$data)
    {
        if(!is_string($method)) {
            return ['code'=>400,'message'=>"发送参数异常",'data'=>[]];
        }
        $rpc = self::getUrl();
        $temp = explode('_', $method);
        if(!isset($temp[0]) || !isset($temp[1])) {
            return ['code'=>400,'message'=>"发送参数异常",'data'=>[]];
        }
        $url = $temp[0];
        $method = $temp[1];
        try {
            $yar_client = new Lib_Yar("{$rpc}/SMS/{$url}.php");
            $res = $yar_client->execute($method, array($data));
            if (empty($res) || !isset($res['code']) || !isset($res['message'])) {
                //记录日志
                Func_Log::write('LrqApi', $url . @http_build_query($res), $res);
                //去错误页面
                return ['code'=>400,'message'=>"发送失败",'data'=>[]];
            }
            return $res;
        } catch (Exception $e) {
            return ['code'=>500,'message'=>"服务器异常",'data'=>[$e->getMessage()]];

        }
    }


}
